<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	积分、余额日志
*/
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>2);
	$return['params']=array();
	//会员ID
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//时间范围
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getslice('jy_commission_yuelog',$params,array($pindex,$psize),$total,array(),'','id desc');
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['agent']=cfc('core')->member_show($item['agent_id']);
		$item['num']=$this->count('commission',array('type'=>31,'ordersn'=>$item['month'],'uniacid'=>$_SESSION['uniacid']));
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'生成记录'),
			array('op'=>'up','title'=>'生成分红'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'month','title'=>'月度','type'=>'string'),
			array('name'=>'num','title'=>'人数','type'=>'string'),
			array('name'=>'allred','title'=>'金额','type'=>'string'),
			array('name'=>'allfee','title'=>'总业绩','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'fa','message'=>'确定发放分红?发放后，分红可直接进客户资金池','title'=>'发放','show'=>array('status'=>0)),
			array('type'=>'href','op'=>'mlist','title'=>'明细'),
		)),
		'search'=>array(
			array('type'=>'month','width'=>15,'title'=>'月度','type'=>'string'),
		),
	);
	$return['status']=array('待发放','已发放');
	exi($return);
}
if($_RQ['op']=='fa'){
	$id=$_RQ['id'];
	$yuelog=pdo_get('jy_commission_yuelog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	pdo_update('jy_commission_yuelog',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	pdo_update('jy_commission_commission',array('status'=>1),array('status'=>0,'type'=>31,'ordersn'=>$yuelog['month'],'uniacid'=>$_SESSION['uniacid']));
	exi('发放成功','','list');	
}
if($_RQ['op']=='up'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$month=$postdata['y'].'-'.$postdata['m'];
		$this->model('model')->upyuelog2($month);
		exi('生成成功','','list');
	}
	$y=$_RQ['y']?:date('Y');
	$m=$_RQ['m']?:date('m')-1;
	if($m<10){
		$m='0'.$m;
	}
	$month = $y.'-'.$m;
	$return['item']=array('y'=>$y,'m'=>$m);
	$nowy=date('Y');
	$htmls=array(
		'type'=>'edit',
		'desc'=>'生成',
		'edit'=>array(
			array('name'=>'y','title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','title'=>'月份','type'=>'radio','options'=>'months','show'=>array('y <'=>$nowy)),
			array('name'=>'m','title'=>'月份','type'=>'radio','options'=>'months2','show'=>array('y'=>$nowy)),
		),
	);
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$months=array();
	for($i=1;$i<=12;$i++){
		$key=$i>9?$i:'0'.$i;
		$months[$key]=$i.'月';
	}
	$return['months']=$months;
	$months2=array();
	$yuestart = mktime(0,0,0,date('m'),1,date('Y'));
	for($i=1;$i<=12;$i++){
		$key=$i>9?$i:'0'.$i;
		if(strtotime($nowy.'-'.$key)>$yuestart-1000){
			break;
		}
		$months2[$key]=$i.'月';
	}
	$return['months2']=$months2;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='mlist'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>31);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['id']){
		$ordersn=pdo_getcolumn('jy_commission_yuelog',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'month');
		$params['ordersn']=trim($ordersn);
		$return['params']['id']=$_RQ['id'];
	}else{
		exi('情选择月份！','error');
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'order_price desc',
	);
	$list=$this->get_list('commission',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$order=pdo_get('jy_weishop_order',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),'status');
			if($order['status']==3 && !$item['status']){
				$this->update('commission',array('status'=>1),array('id'=>$item['id'],'status'=>0,'uniacid'=>$_SESSION['uniacid']));
			}
			$item['level']=$item['type']?:$item['level'];
			if($item['is_sale']){
				$item['level']='sale'.$item['level'];
			}
			if($item['level']==25 && $item['isxuni']){
				$this->update('commission',array('isxuni'=>0),array('id'=>$item['id'],'level'=>25,'uniacid'=>$_SESSION['uniacid']));
			}
			$item['remark']=json_decode($item['remark'],true);
			$item['outfee']=$item['remark']['outfee']?:'0.00';
			$item['out']=$item['remark']['out']?:'';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回生成记录'),
		),
		'list'=>array(
			'data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'分销商','type'=>'member'),
			array('name'=>'ordersn','title'=>'月份','type'=>'string'),
			array('name'=>'level','title'=>'奖励类型','type'=>'radio','options'=>'commission_money_types'),
			array('name'=>'order_price','title'=>'计算业绩','type'=>'string'),
			array('name'=>'money','title'=>'佣金','type'=>'string'),
			array('name'=>'out','title'=>'下级扣除','type'=>'string'),
			array('name'=>'jifen','title'=>'积分','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'status','title'=>'佣金状态','type'=>'radio','options'=>'commission_status'),
			array('name'=>'complete_time','title'=>'订单完成时间','type'=>'time'),
			array('name'=>'balancesn','title'=>'结算单号','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'佣金状态','options'=>'commission_status'),
		),
	);
	$return['htmls']=$htmls;
	$return['commission_status']=$this->commission_status2;
	$return['commission_money_types']=$this->commission_money_types2;
	exi($return);
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		$ordersn=pdo_getcolumn('jy_commission_yuelog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'month');
		pdo_delete('jy_commission_yuelog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		pdo_delete('jy_commission_commission',array('status'=>array(0,1),'type'=>31,'ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$ordersn=pdo_getcolumn('jy_commission_yuelog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'month');
				pdo_delete('jy_commission_yuelog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				pdo_delete('jy_commission_commission',array('status'=>array(0,1),'type'=>31,'ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}